// 仅使用两个栈实现先入先出队列

// 思路
// 使用两个栈，inStack用于输入，outStack用于输出
// push，将元素压入instack
// pop，如果outStack输出栈为空，将inStack输入栈元素依次取出，按顺序压入outStack栈，outStack顶层元素就是要取出的队头元素
// peek，有pop类似，不需要取出顶层元素，直接将其返回即可
// empty，如果inStack和outStack都为空，则队列为空，否则队列不为空

let MyQueue = function() {
    this.inStack = []
    this.outStack = []
};

/** 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.inStack.push(x)
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if (this.outStack.length === 0) {
        while (this.inStack.length !== 0) {
            this.outStack.push(this.inStack.pop())
        }
    }
    return this.outStack.pop()
};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    if (this.outStack.length === 0) {
        while (this.inStack.length !== 0) {
            this.outStack.push(this.inStack.pop())
        }
    }
    return this.outStack[this.outStack.length - 1]
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return this.outStack.length === 0 && this.inStack.length === 0
};

let myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
console.log(myQueue.peek()); // return 1
console.log(myQueue.pop()); // return 1, queue is [2]
console.log(myQueue.empty()); // return false
